/*TourOffCMD.java
 * 
 * this command creates a new tour offering and adds it to the
 * collection of tour offerings within the tourApp.
 * tourOff input data: (tour,offeringName,startDate).
 *
 * Greg Warner
 * Derek Dupuis
 * Michael Dixey
 * Alex Schwartz
 */

import java.util.*;

public class TourOffCMD extends Cmd
{
   //integers for start date
   public int tourOffYearStart;
   public int tourOffMonthStart;
   public int tourOffDayStart;
   
   public TourOffCMD() 
   { 
     super(); 
   }
   
   /*make new tourOff in TourApp*/
   
   public void exec()
   {
     if( token.length >= 4 )
     {
        
        int g; //start date
        try{ g =Integer.parseInt( token[ 3 ] ); }
        catch( NumberFormatException e )
        {
           System.out.printf( " bad int %s\n", token[3], e.getMessage( ));
           return;
        } 
        
        //tour object(name)
        String tourName = token[ 1 ];
        Tour t = tourApp.getTour(tourName);

        //unique offeringName
        String offeringName = token[ 2 ];
        TourOff tO = tourApp.getTourOffering( offeringName );
        //check to make sure offeringName doesn't already exist
        if( tO != null )
        {
           System.err.printf( "Tour offering with name %s already exists\n",
                              offeringName );
           return;
        }
        //getting indiviual day/month/year from greg. cal object.
        tourOffYearStart = new Integer( token[3].substring( 0,2 ) );
        tourOffMonthStart = new Integer( token[3].substring( 2,4 ) );
        tourOffDayStart = new Integer( token[3].substring( 4 ) );     
        tourOffYearStart += 2000;
          
      
        GregorianCalendar tourOffStartDate = new GregorianCalendar( 
                                  tourOffYearStart, tourOffMonthStart - 1, tourOffDayStart );
        //validity check
        if( t != null &&  t.validityCheck() )
        {
           //if valid, make new tour offering
           TourOff newTourOff = new TourOff( t, offeringName, tourOffStartDate);
           t.lockTour();
           tourApp.addTourOffering( newTourOff );
        }
        else
        {
           if( t == null )
           {
              System.err.printf( "Tour doesn't exist\n" );
              return;
           }
           else
           {
              System.err.printf( "Tour not valid!\n" );
              return;
           }   
        }
        
     }
     else
     {
       System.err.print( "Not enough tokens on tourOff line!\n" );
       return;
     }
   }
}

